package org.example.Soution002;

public class FindeRange {
    public static void main(String[] args) {
        int[] nums=new int[]{5,7,7,8,8,10};
        FindeRange f=new FindeRange();
        int[] res=f.searchRange(nums,6);
        for(int num:res){
            System.out.print(num+" ");
        }
    }

    public int[] searchRange(int[] nums, int target) {
        int[] res=new int[]{-1,-1};
        //第一个二分锁定一个答案
        int start=0,end=nums.length-1;
        while(start<=end){
            int mid=(start+end)/2;
            if(nums[mid]==target){
                getRange(nums,res,target,mid);
                break;
            }else if(nums[mid]<target){
                start=mid+1;
            }else{
                end=mid-1;
            }
        }
        return res;
    }

    public void getRange(int[] nums,int[] res,int target,int midRes){
        int begin=0,end=nums.length-1;
        double tag=target-0.5;
        res[0]=binarySearchRow(nums,begin,midRes,target-0.5,target);
        res[1]=binarySearchRow(nums,midRes,end,target+0.5,target);
    }

    public int binarySearchRow(int[] nums,int low,int high,double tag,int target){
        while(low<=high){
            int mid=(low+high)/2;
            if(nums[mid]==tag){
                System.out.println("不可能的");
            }else if(nums[mid]<tag){
                low=mid+1;
            }else{
                high=mid-1;
            }
        }
        return tag<target?low:high;
    }
}
